package com.ruoyi.web.controller.system;

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.SysMember;
import com.ruoyi.system.domain.vo.MemberVo;
import com.ruoyi.system.service.ISysMemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 会员管理Controller
 * 
 * @author ruoyi
 * @date 2025-10-08
 */
@RestController
@RequestMapping("/system/member")
@Anonymous
public class SysMemberController extends BaseController
{

    @Autowired
    private ISysMemberService sysMemberService;

    /**
     * 查询会员管理列表
     */
    //@PreAuthorize("@ss.hasPermi('system:member:list')")
    @RequestMapping("/list")
    public TableDataInfo list(SysMember sysMember)
    {
        startPage();
        //List<SysMember> list = sysMemberService.selectSysMemberList(sysMember);
        List<MemberVo> list = sysMemberService.selectMemberList(sysMember);
        return getDataTable(list);
    }

    /**
     * 根据用户Id获取详细信息
     */
    //@PreAuthorize("@ss.hasPermi('system:member:query')")
    @GetMapping(value = "/{memberId}")
    public AjaxResult getInfo(@PathVariable Long memberId)
    {
        return success(sysMemberService.selectSysMemberById(memberId));
    }

    /**
     * 导出会员管理列表
     */
    //@PreAuthorize("@ss.hasPermi('system:member:export')")
    @Log(title = "会员管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysMember sysMember)
    {
        List<SysMember> list = sysMemberService.selectSysMemberList(sysMember);
        ExcelUtil<SysMember> util = new ExcelUtil<SysMember>(SysMember.class);
        util.exportExcel(response,list, "会员管理数据");
    }

    /**
     * 新增保存会员管理
     */
   // @PreAuthorize("@ss.hasPermi('system:member:add')")
    @Log(title = "会员管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult addSave(@Validated @RequestBody SysMember sysMember)
    {
        return toAjax(sysMemberService.insertSysMember(sysMember));
    }

    /**
     * 修改保存会员管理
     */
    //@PreAuthorize("@ss.hasPermi('system:member:edit')")
    @Log(title = "会员管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public AjaxResult editSave(@Validated @RequestBody SysMember sysMember)
    {
        return toAjax(sysMemberService.updateSysMember(sysMember));
    }

    /**
     * 删除会员管理
     */
    //@PreAuthorize("@ss.hasPermi('system:member:remove')")
    @Log(title = "会员管理", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    public AjaxResult remove(String ids)
    {
        return toAjax(sysMemberService.deleteSysMemberByIds(ids));
    }
}
